# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=rnnoise
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.2
pkgrel=2
pkgdesc="Recurrent neural network for audio noise reduction (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://gitlab.xiph.org/xiph/rnnoise'
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
_model_version='0b50c45' # cat model_version
source=("https://gitlab.xiph.org/xiph/rnnoise/-/archive/v${pkgver}/${_realname}-v${pkgver}.tar.gz"
        "https://media.xiph.org/rnnoise/models/rnnoise_data-${_model_version}.tar.gz"
        "001-os-support.patch::https://gitlab.xiph.org/xiph/rnnoise/-/commit/372f7b4b76cde4ca1ec4605353dd17898a99de38.patch")
sha256sums=('fafc947fdd24109a6e72b5f25e4224b54bc74660a2620af5548def85be8c733a'
            '4ac81c5c0884ec4bd5907026aaae16209b7b76cd9d7f71af582094a2f98f4b43'
            '03ef15484795607274840e4c0167a4f59173357395985f73cc1c940fe4bcd0b0')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  mv src/* "${_realname}-v${pkgver}/src" && cd "${_realname}-v${pkgver}"

  apply_patch_with_msg \
    001-os-support.patch

  autoreconf -isf
}

build() {
  declare -a _extra_config

  if [[ ${CARCH} != aarch64 ]]; then
    _extra_config+=("--enable-x86-rtcd")
  fi

  mkdir -p "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  ../"${_realname}-v${pkgver}"/configure \
    --prefix="${MINGW_PREFIX}" \
    "${_extra_config[@]}"

  make
}

package() {
  make -C "build-${MSYSTEM}" install DESTDIR="${pkgdir}"

  install -Dm644 "${_realname}-v${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
